package com.atguigu.srb.sms.receiver;

import com.atguigu.srb.mq.pojo.dto.SmsDTO;
import com.atguigu.srb.sms.service.SmsService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class SmsReceiver {

    @Autowired
    SmsService smsService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value="exchange.recharge.sms",durable = "true"),
            key={"routing.recharge.sms"},
            value= @Queue(value="queue.recharge.sms",durable = "true")
    ))
    public void smsSend(Channel channel, Message message, SmsDTO smsDTO){
        byte[] body = message.getBody();
        String mes = new String(body);

        System.out.println("sms消费，发送充值短信");

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//commit
        } catch (IOException e) {
            e.printStackTrace();
        }
        //channel.basicNack();//rollback
    }

}
